<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报工人" prop="paperWorkName">
        <el-input
          v-model="queryParams.paperWorkName"
          placeholder="请输入报工人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报工日期" prop="paperWorkDate">
        <el-date-picker clearable
                        v-model="queryParams.paperWorkDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择报工日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
        >
          <el-option
            v-for="dict in dict.type.review_worker_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['third:paperWorker:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['third:paperWorker:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['third:paperWorker:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['third:paperWorker:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperWorkerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="报工单主键" align="center" prop="id"/>
      <el-table-column label="报工人" align="center" prop="paperWorkName"/>
      <el-table-column label="报工日期" align="center" prop="paperWorkDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paperWorkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报工产品" align="center" prop="paperWorkProduct"/>
      <el-table-column label="报工数量" align="center" prop="paperWorkNum"/>
      <el-table-column label="单价" align="center" prop="unitPrice"/>
      <el-table-column label="总价" align="center" prop="totalPrice"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.review_worker_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
        <template slot-scope="scope">
          <el-button icon="el-icon-view" type="text" size="mini" @click="handleView(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['third:paperWorker:edit']"
          >修改
          </el-button>
          <el-button
            v-if="scope.status==='0'"
            size="mini"
            type="text"
            icon="el-icon-reading"
            @click="handleReview(scope.row)"
          >审核
          </el-button>
          <el-button
            v-if="scope.status==='0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['third:paperWorker:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报工单对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报工人" prop="paperWorkName">
          <el-input disabled v-model="form.paperWorkName" placeholder="请输入报工人"/>
        </el-form-item>
        <el-form-item label="报工产品" prop="paperWorkProduct">
          <el-select
            v-model="form.paperWorkProduct"
            filterable
            :remote="true"
            placeholder="请输入商品名称"
            style="width: 86%"
            :remote-method="querySearch"
            @change="handleSelectInput"
            :loading="productLoading"
          ></el-select>
          <i
            class="el-icon-search"
            v-if="!isView"
            @click="showSelectProductDialog = true"
            style="
                margin-left: 10px;
                font-size: 20px;
                font-weight: bold;
                cursor: pointer;
              "
          />
        </el-form-item>
        <el-form-item label="报工数量" prop="paperWorkNum">
          <el-input v-model="form.paperWorkNum" placeholder="请输入报工数量"/>
        </el-form-item>
        <el-form-item label="报工日期" prop="paperWorkDate">
          <el-date-picker clearable
                          v-model="form.paperWorkDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择报工日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input :disabled="isReview"  @input="calculate(form.unitPrice)" v-model="form.unitPrice" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input :disabled="isReview" v-model="form.totalPrice" placeholder="请输入总价"/>
        </el-form-item>
        <el-form-item label="审核结果">
          <el-radio-group v-model="form.status">
            <el-radio label="1" value="1">审核通过</el-radio>
            <el-radio label="2" value="2">审核拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" width="1100px" :visible.sync="showSelectProductDialog">
      <mditem :isComponent="true" @select="onSelectGoods"/>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPaperWorker,
  getPaperWorker,
  delPaperWorker,
  addPaperWorker,
  updatePaperWorker
} from '@/api/third/paperWorker'
import mditem from '@/views/mes/md/mditem/index.vue'

export default {
  name: 'PaperWorker',
  components: { mditem },
  dicts: ['review_worker_status'],
  data() {
    return {
      roles:[],
      // 遮罩层
      loading: true,
      isReview: true,
      productLoading: false,
      showSelectProductDialog: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报工单表格数据
      paperWorkerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      isView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperWorkName: null,
        paperWorkDate: null,
        paperWorkProduct: null,
        paperWorkNum: null,
        unitPrice: null,
        totalPrice: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.roles = this.$store.getters.roles
    //判断是不是报工审核员
    if ( this.roles.includes('reviewWorker')) {
      this.isReview = false
    }
  },
  methods: {
    /** 查询报工单列表 */
    getList() {
      this.loading = true
      listPaperWorker(this.queryParams).then(response => {
        this.paperWorkerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    querySearch(query) {
    },
    handleSelectInput() {
    },
    //计算总价
    calculate(unitPrice){
      this.form.totalPrice = unitPrice * this.form.paperWorkNum
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        paperWorkName: null,
        paperWorkDate: null,
        paperWorkProduct: null,
        paperWorkNum: null,
        unitPrice: null,
        totalPrice: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isView = false
      this.reset()
      this.form.paperWorkName = this.$store.getters.name
      this.open = true
      this.title = '添加报工单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPaperWorker(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改报工单'
      })
    },
    handleReview(row) {
      this.reset()
      const id = row.id || this.ids
      getPaperWorker(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '审核报工单'
      })
    },
    handleView(row) {
      this.isView = true
      const id = row.id
      getPaperWorker(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看报工单'
      })
    },
    onSelectGoods(goods) {
      this.showSelectProductDialog = false
      this.form.paperWorkProduct = goods.itemName
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePaperWorker(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPaperWorker(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除报工单编号为"' + ids + '"的数据项？').then(function() {
        return delPaperWorker(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('third/paperWorker/export', {
        ...this.queryParams
      }, `paperWorker_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
